package com.hss01248.dialog;

import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;

import java.util.Optional;

/**
 * Created by huangshuisheng on 2017/10/26.
 */

public class ScreenUtil {
    /**
     * 获取屏幕尺寸，但是不包括虚拟功能高度
     *
     * @return
     */
    public static int getScreenHeight() {
        Optional<Display> display = DisplayManager.getInstance().getDefaultDisplay(StyledDialog.context);
        DisplayAttributes attrs = display.get().getAttributes();
        return attrs.height;
    }

    /**
     * 通过反射，获取包含虚拟键的整体屏幕高度
     *
     * @return
     */
    public static int getgetScreenHeightHasVirtualKey() {
        int dpi = 0;
        return dpi;
    }

    public static DisplayManager getWindowManager() {
        return DisplayManager.getInstance();
    }

    public static int px2dip( float pxValue) {
        Optional<Display> display = DisplayManager.getInstance().getDefaultDisplay(StyledDialog.context);
        DisplayAttributes attrs = display.get().getAttributes();
        final float scale = attrs.scalDensity;
        return (int) (pxValue / scale + 0.5f);
    }

    public static int dip2px( float dipValue)
    {
        Optional<Display> display = DisplayManager.getInstance().getDefaultDisplay(StyledDialog.context);
        DisplayAttributes attrs = display.get().getAttributes();
        final float scale = attrs.scalDensity;
        return (int) (dipValue * scale + 0.5f);
    }

    /**
     * 将sp值转换为px值，保证文字大小不变
     *
     * @param spValue
     * @param fontScale（DisplayMetrics类中属性scaledDensity）
     * @return
     */
    private static float sp2px(float spValue, float fontScale) {
        return  (spValue * fontScale + 0.5f);
    }
    /**
     * 将sp值转换为px值，保证文字大小不变
     *

     * @return
     */
    public static float sp2px(float spValue) {
        Optional<Display> display = DisplayManager.getInstance().getDefaultDisplay(StyledDialog.context);
        DisplayAttributes attrs = display.get().getAttributes();
        final float scale = attrs.scalDensity;
        return sp2px( spValue,  scale);
    }
}
